package cn.zhf.shop.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.zhf.shop.config.GlobalConfig;
import cn.zhf.shop.sdk.MyConfig;
import cn.zhf.shop.sdk.WXPay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class H5WXPayUtils {

    private static MyConfig config;
    private Logger log= LoggerFactory.getLogger(getClass());


    static {
        try {
            config = new MyConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static WXPay wxpay;

    static {
        try {
            wxpay = new WXPay(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Author: 王浩伟
     * @Date: 22:58 2020/6/30
     * @Description:  H5向微信支付接口统一下单
     */
    public static String H5unifiedOrder(String orderId,String goodsName,int price,String UserIP) throws Exception{
        Map data = new HashMap<String, String>();

        data.put("body",goodsName);
        data.put("out_trade_no",orderId);
        data.put("total_fee",price);
        data.put("spbill_create_ip",UserIP);
        data.put("notify_url","https://qiudaokeji.cn/shop/api/orders/notify/H5");
        data.put("trade_type","JSAPI");
        data.put("profit_sharing","Y");
        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println("统一下单结果："+ JSONUtil.parseObj(resp));
            JSONObject json=JSONUtil.parseObj(resp);
            if (json.getStr("return_code").equals("SUCCESS")&&json.getStr("result_code").equals("SUCCESS")) {
                String prepay_id = resp.get("prepay_id");
                String mweb_url = resp.get("mweb_url");

                return mweb_url;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
